package com.beidouin.GnssMain.fragment;

/**
 * Created by yuyang on 16/6/23.
 */
/*
 * Copyright (C) 2008-2013 The Android Open Source Project,
 * Sean J. Barbeau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.beidouin.GnssMain.adapter.SatelliteAdapter;
import com.beidouin.GnssMain.models.BeidouSatellite;
import com.beidouin.GnssMain.models.BeidouStatus;
import com.beidouin.GnssMain.models.GnssSatellite;
import com.beidouin.GnssMain.models.PilotInfo;
import com.beidouin.GnssMain.R;

import java.util.HashMap;
import java.util.Iterator;

public class GpsSkyFragment extends Fragment {

    protected final String TAG = this.getClass().getName() + "@" + this.hashCode();

    TextView tv_all, tv_gps, tv_beidou, tv_glonass, tv_galileo, tv_qzss;
    TextView tv_all1, tv_gps1, tv_beidou1, tv_glonass1, tv_galileo1, tv_qzss1;
    TextView tv_all2, tv_gps2, tv_beidou2, tv_glonass2, tv_galileo2, tv_qzss2;
    TextView tv_all3, tv_gps3, tv_beidou3, tv_glonass3, tv_galileo3, tv_qzss3;
    public static int mHeight;

    public static int mWidth;
    private GpsSkyView mSkyView;
    static private Bitmap[][] bmpSatellite;
    private HashMap<String, Integer> bright;
    private HashMap<String, Integer> all;
    private int beidou = 0;
    private int beidou1 = 0;
    private int gps = 0;
    private int gps1 = 0;
    private int glonass = 0;
    private int glonass1 = 0;
    private int galileo = 0;
    private int galileo1 = 0;
    private int qzss = 0;
    private int qzss1 = 0;
    private int all_zong;
    private int all_zong1;

    private Integer sate_gps;
    private Integer sate_gps1;
    private Integer sate_beidou;
    private Integer sate_beidou1;
    private Integer sate_glonass;
    private Integer sate_glonass1;
    private Integer sate_galileo;
    private Integer sate_galileo1;
    private Integer sate_qzss;
    private Integer sate_qzss1;
    private SatelliteAdapter mAdapter;

    // newInstance constructor for creating fragment with arguments
    public static GpsSkyFragment newInstance(int page, String title) {
        GpsSkyFragment fragmentFirst = new GpsSkyFragment();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);


        return fragmentFirst;
    }

    public void validateUI() {
        BeidouStatus.getInstance().setData(PilotInfo.getBeidouSatelliteHashMap());
        mSkyView.setSats(BeidouStatus.getInstance());
        updateSatelliteNumber();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View parentView = inflater.inflate(R.layout.fragment_sky, container, false);

        RelativeLayout skyRelative = (RelativeLayout) parentView.findViewById(R.id.sky_rl);

        mAdapter = new SatelliteAdapter(getActivity());
        bmpSatellite = new Bitmap[GnssSatellite.SATE_TOTAL][2];
        bmpSatellite[GnssSatellite.SATE_GPS][0] = BitmapFactory.decodeResource(getResources(), R.drawable.gps);
        bmpSatellite[GnssSatellite.SATE_GPS][1] = BitmapFactory.decodeResource(getResources(), R.drawable.gps2);
        bmpSatellite[GnssSatellite.SATE_GLONASS][0] = BitmapFactory.decodeResource(getResources(), R.drawable.glonass);
        bmpSatellite[GnssSatellite.SATE_GLONASS][1] = BitmapFactory.decodeResource(getResources(), R.drawable.glonass2);
        bmpSatellite[GnssSatellite.SATE_GALILEO][0] = BitmapFactory.decodeResource(getResources(), R.drawable.galileo);
        bmpSatellite[GnssSatellite.SATE_GALILEO][1] = BitmapFactory.decodeResource(getResources(), R.drawable.galileo2);
        bmpSatellite[GnssSatellite.SATE_BEIDOU][0] = BitmapFactory.decodeResource(getResources(), R.drawable.beidou);
        bmpSatellite[GnssSatellite.SATE_BEIDOU][1] = BitmapFactory.decodeResource(getResources(), R.drawable.beidou2);
        bmpSatellite[GnssSatellite.SATE_QZSS][0] = BitmapFactory.decodeResource(getResources(), R.drawable.qzss);
        bmpSatellite[GnssSatellite.SATE_QZSS][1] = BitmapFactory.decodeResource(getResources(), R.drawable.qzss2);

        tv_beidou = (TextView) parentView.findViewById(R.id.num_beidou);
        tv_gps = (TextView) parentView.findViewById(R.id.num_gps);
        tv_glonass = (TextView) parentView.findViewById(R.id.num_glonass);
        tv_galileo = (TextView) parentView.findViewById(R.id.num_galileo);
        tv_qzss = (TextView) parentView.findViewById(R.id.num_qzss);
        tv_all = (TextView) parentView.findViewById(R.id.num_all);

        tv_beidou1 = (TextView) parentView.findViewById(R.id.num_beidou1);
        tv_gps1 = (TextView) parentView.findViewById(R.id.num_gps1);
        tv_glonass1 = (TextView) parentView.findViewById(R.id.num_glonass1);
        tv_galileo1 = (TextView) parentView.findViewById(R.id.num_galileo1);
        tv_qzss1 = (TextView) parentView.findViewById(R.id.num_qzss1);
        tv_all1 = (TextView) parentView.findViewById(R.id.num_all1);

        tv_beidou2 = (TextView) parentView.findViewById(R.id.num_beidou2);
        tv_gps2 = (TextView) parentView.findViewById(R.id.num_gps2);
        tv_glonass2 = (TextView) parentView.findViewById(R.id.num_glonass2);
        tv_galileo2 = (TextView) parentView.findViewById(R.id.num_galileo2);
        tv_qzss2 = (TextView) parentView.findViewById(R.id.num_qzss2);
        tv_all2 = (TextView) parentView.findViewById(R.id.num_all2);

        tv_beidou3 = (TextView) parentView.findViewById(R.id.num_beidou3);
        tv_gps3 = (TextView) parentView.findViewById(R.id.num_gps3);
        tv_glonass3 = (TextView) parentView.findViewById(R.id.num_glonass3);
        tv_galileo3 = (TextView) parentView.findViewById(R.id.num_galileo3);
        tv_qzss3 = (TextView) parentView.findViewById(R.id.num_qzss3);
        tv_all3 = (TextView) parentView.findViewById(R.id.num_all3);


        tv_beidou1.setText("BDS");
        tv_gps1.setText("GPS");
        tv_galileo1.setText("GALILEO");
        tv_glonass1.setText("GLONASS");
        tv_qzss1.setText("QZSS");
        tv_all1.setText("总共");


        mSkyView = new GpsSkyView(getActivity());
        skyRelative.addView(mSkyView);
        // Get the proper height and width of this view, to ensure the compass draws onscreen
        mSkyView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    @SuppressWarnings("deprecation")
                    @SuppressLint("NewApi")
                    @Override
                    public void onGlobalLayout() {
                        final View v = getView();
                        mWidth = v.getHeight();
                        mHeight = v.getWidth();

                        if (v.getViewTreeObserver().isAlive()) {
                            // remove this layout listener
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                                v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                            } else {
                                v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                            }
                        }
                    }
                }
        );
        mSkyView.setSats(BeidouStatus.getInstance());
        validateUI();
        updateSatelliteNumber();
        return parentView;
    }

    private void updateSatelliteNumber() {

        bright = SatelliteAdapter.getBright();
        all = SatelliteAdapter.getDestroy();

        sate_beidou = 0;
        sate_beidou1 = 0;
        sate_beidou = bright.get("SATE_BEIDOU");
        sate_beidou1 = all.get("SATE_BEIDOU");
        if (sate_beidou == null ) {
            tv_beidou2.setText("0");
            tv_beidou3.setText("0");
            beidou = 0;
            beidou1 = 0;

        } else {
            beidou = sate_beidou;
            tv_beidou2.setText(sate_beidou + "");
            tv_beidou3.setText(sate_beidou + "");
            if (sate_beidou1 != null){
                beidou1 = beidou + beidou1;
                tv_beidou3.setText(sate_beidou + sate_beidou1 +"");
                beidou1 = sate_beidou + sate_beidou1;
            }else {
                beidou1 = sate_beidou;

            }
        }

        Drawable iconBeidou = getResources().getDrawable(R.drawable.beidou);
        iconBeidou.setBounds(0, 0, 50, 50);
        tv_beidou.setCompoundDrawables(iconBeidou, null, null, null);


        sate_gps = bright.get("SATE_GPS");
        sate_gps1 = all.get("SATE_GPS");
        if (sate_gps == null) {
            tv_gps2.setText("0");
            tv_gps3.setText("0");
            gps = 0;
            gps1 = 0;
        } else {
            gps = sate_gps;
            gps1 = sate_gps;
            tv_gps2.setText(sate_gps + "");
            tv_gps3.setText(sate_gps + "");

            if (sate_gps1 != null){
                gps1 = sate_gps + sate_gps1;
                tv_gps3.setText(sate_gps + sate_gps1 + "");
            }

        }
//        if (){
//            tv_gps3.setText("0");
//            gps1 = 0;
//        }else {
//            tv_gps3.setText(sate_gps1 + gps + "");
//            gps1 = gps + sate_gps1;
//        }
        Drawable iconGps = getResources().getDrawable(R.drawable.gps);
        iconGps.setBounds(0, 0, 50, 50);
        tv_gps.setCompoundDrawables(iconGps, null, null, null);


        sate_glonass = bright.get("SATE_GLONASS");
        sate_glonass1 = all.get("SATE_GLONASS");
        if (sate_glonass == null) {
            tv_glonass2.setText("0");
            tv_glonass3.setText("0");
            glonass1 = 0;
            glonass = 0;
        } else {

            glonass = sate_glonass;
            glonass1 = sate_glonass ;
            tv_glonass2.setText(sate_glonass + "");
            tv_glonass3.setText(sate_glonass + "");
            if (sate_glonass1 != null){
                glonass1 = sate_glonass + sate_glonass1;
                tv_glonass3.setText(sate_glonass + sate_glonass1 + "");
            }
        }
//        if (sate_glonass1 == null){
//
//        }else {
//            tv_glonass3.setText(sate_glonass + sate_glonass1 + "");
//            glonass1 = sate_glonass + sate_glonass1;
//        }
        Drawable iconGlonass = getResources().getDrawable(R.drawable.glonass);
        iconGlonass.setBounds(0, 0, 50, 50);
        tv_glonass.setCompoundDrawables(iconGlonass, null, null, null);

        sate_galileo = bright.get("SATE_GALILEO");
        sate_galileo1 = all.get("SATE_GALILEO");
        if (sate_galileo == null) {
            tv_galileo2.setText("0");
            tv_galileo3.setText("0");
            galileo = 0;
            galileo1 = 0;
        } else {
            galileo = sate_galileo;
            galileo1 = sate_galileo;
            tv_galileo2.setText(sate_galileo + "");
            tv_galileo3.setText(sate_galileo + "");
            if (sate_galileo1 != null){
                galileo1 = sate_galileo + sate_galileo1;
                tv_galileo3.setText(sate_galileo + sate_galileo1 + "");

            }
        }
        Drawable iconGalileo = getResources().getDrawable(R.drawable.galileo);
        iconGalileo.setBounds(0, 0, 50, 50);
        tv_galileo.setCompoundDrawables(iconGalileo, null, null, null);


        sate_qzss = bright.get("SATE_QZSS");
        sate_qzss1 = all.get("SATE_QZSS");
        if (sate_qzss == null) {
            tv_qzss2.setText("3");
            qzss = 3;
        } else {
            tv_qzss2.setText(3+ "");
            qzss = 3;
        }

        if (sate_qzss1 == null){
            tv_qzss3.setText("0");
            sate_qzss = 0;
            sate_qzss1 = 0;
            qzss1 = sate_qzss + sate_qzss1;
        }else {
            tv_qzss3.setText( 3 + "");
            qzss1 =  3;

        }
        Drawable iconqzss = getResources().getDrawable(R.drawable.qzss);
        iconqzss.setBounds(0, 0, 50, 50);
        tv_qzss.setCompoundDrawables(iconqzss, null, null, null);


        all_zong = beidou + gps + qzss + glonass + galileo;
        all_zong1 = beidou1 + gps1 + qzss1 + glonass1 + galileo1;


        tv_all2.setText(all_zong + "");
        tv_all3.setText(all_zong1 + "");
        Drawable iconAll = getResources().getDrawable(R.drawable.sate_all);
        iconAll.setBounds(0, 0, 50, 50);
        tv_all.setCompoundDrawables(iconAll, null, null, null);
    }

    private static class GpsSkyView extends View {

        private static final int SAT_RADIUS = 10;

        private static final float PRN_TEXT_SCALE = 2.5f;

        private final float mSnrThresholds[];

        private final int mSnrColors[];

        Context mContext;

        WindowManager mWindowManager;

        private Paint mHorizonActiveFillPaint, mHorizonInactiveFillPaint, mHorizonStrokePaint, mGridStrokePaint,
                mSatelliteFillPaint, mSatelliteStrokePaint, mNorthPaint, mNorthFillPaint, mPrnIdPaint, mPrnNumPaint;

        private double mOrientation = 0.0;

        private boolean mStarted;

        private int mSnrs[], mElevs[], mAzims[], mSateType[];

        private boolean mUsed[];

        private int msvId[];

        private int mSvCount;
        private Rect rect;

        public GpsSkyView(Context context) {
            super(context);

            mContext = context;
            mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

            mHorizonActiveFillPaint = new Paint();
            mHorizonActiveFillPaint.setColor(Color.WHITE);
            mHorizonActiveFillPaint.setStyle(Paint.Style.FILL);
            mHorizonActiveFillPaint.setAntiAlias(true);

            mHorizonInactiveFillPaint = new Paint();
            mHorizonInactiveFillPaint.setColor(Color.LTGRAY);
            mHorizonInactiveFillPaint.setStyle(Paint.Style.FILL);
            mHorizonInactiveFillPaint.setAntiAlias(true);

            mHorizonStrokePaint = new Paint();
            mHorizonStrokePaint.setColor(Color.BLACK);
            mHorizonStrokePaint.setStyle(Paint.Style.STROKE);
            mHorizonStrokePaint.setStrokeWidth(2.0f);
            mHorizonStrokePaint.setAntiAlias(true);

            mGridStrokePaint = new Paint();
            mGridStrokePaint.setColor(Color.GRAY);
            mGridStrokePaint.setStyle(Paint.Style.STROKE);
            mGridStrokePaint.setAntiAlias(true);
            mGridStrokePaint.setPathEffect(new DashPathEffect(new float[]{10, 20}, 0));

            mSatelliteFillPaint = new Paint();
            mSatelliteFillPaint.setColor(Color.YELLOW);
            mSatelliteFillPaint.setStyle(Paint.Style.FILL);
            mSatelliteFillPaint.setAntiAlias(true);

            mSatelliteStrokePaint = new Paint();
            mSatelliteStrokePaint.setColor(Color.BLACK);
            mSatelliteStrokePaint.setStyle(Paint.Style.STROKE);
            mSatelliteStrokePaint.setStrokeWidth(2.0f);
            mSatelliteStrokePaint.setAntiAlias(true);

            mSnrThresholds = new float[]{0.0f, 10.0f, 20.0f, 30.0f};
            mSnrColors = new int[]{Color.RED, Color.GREEN, Color.RED, Color.GREEN};

            mNorthPaint = new Paint();
            mNorthPaint.setColor(Color.BLACK);
            mNorthPaint.setStyle(Paint.Style.STROKE);
            mNorthPaint.setStrokeWidth(4.0f);
            mNorthPaint.setAntiAlias(true);

            mNorthFillPaint = new Paint();
            mNorthFillPaint.setColor(Color.GRAY);
            mNorthFillPaint.setStyle(Paint.Style.FILL);
            mNorthFillPaint.setStrokeWidth(4.0f);
            mNorthFillPaint.setAntiAlias(true);

            mPrnIdPaint = new Paint();
            mPrnIdPaint.setColor(Color.BLACK);
            mPrnIdPaint.setStyle(Paint.Style.STROKE);
            //mNorthPaint.setStrokeWidth(4.0f);
            mPrnIdPaint.setTextSize(SAT_RADIUS * PRN_TEXT_SCALE);
            mPrnIdPaint.setAntiAlias(true);

            mPrnNumPaint = new Paint();
            mPrnNumPaint.setColor(Color.BLACK);
            mPrnNumPaint.setStyle(Paint.Style.STROKE);
            //mPrnNumPaint.setStrokeWidth(4.0f);
            mPrnNumPaint.setTextSize(SAT_RADIUS * PRN_TEXT_SCALE / 2);
            mPrnNumPaint.setAntiAlias(true);

            setFocusable(true);
        }

        public void setStarted() {
            mStarted = true;
            invalidate();
        }

        public void setStopped() {
            mStarted = false;
            mSvCount = 0;
            invalidate();
        }

        public void setSats(BeidouStatus status) {
            Iterator<BeidouSatellite> satellites = status.getSatellites().iterator();

            int length = status.getMaxSatellites();
            if (mSnrs == null || (mSnrs.length != length)) {
                mSnrs = new int[length];
                mElevs = new int[length];
                mAzims = new int[length];
                mSateType = new int[length];
                mUsed = new boolean[length];
                msvId = new int[length];
            }

            mSvCount = 0;
            while (satellites.hasNext()) {
                BeidouSatellite satellite = satellites.next();
                if (satellite.getIsReceived() != true) {
                    continue;
                }
                mSnrs[mSvCount] = satellite.getmSnr();
                mElevs[mSvCount] = satellite.getmElevation();
                mAzims[mSvCount] = satellite.getmAzimuth();
                mSateType[mSvCount] = satellite.getSateType();
                mUsed[mSvCount] = satellite.getIsUsed();
                msvId[mSvCount] = satellite.getMsvId();
                mSvCount++;
            }
            mStarted = true;
            invalidate();
        }

        private void drawLine(Canvas c, float x1, float y1, float x2, float y2) {
            // rotate the line based on orientation
            double angle = Math.toRadians(-mOrientation);
            float cos = (float) Math.cos(angle);
            float sin = (float) Math.sin(angle);

            float centerX = (x1 + x2) / 2.0f;
            float centerY = (y1 + y2) / 2.0f;
            x1 -= centerX;
            y1 = centerY - y1;
            x2 -= centerX;
            y2 = centerY - y2;

            float X1 = cos * x1 + sin * y1 + centerX;
            float Y1 = -(-sin * x1 + cos * y1) + centerY;
            float X2 = cos * x2 + sin * y2 + centerX;
            float Y2 = -(-sin * x2 + cos * y2) + centerY;

            c.drawLine(X1, Y1, X2, Y2, mGridStrokePaint);
        }

        private void drawHorizon(Canvas c, int s) {
            float radius = s / 2;

            c.drawCircle(radius, radius, elevationToRadius(s, 90),
                    mStarted ? mHorizonActiveFillPaint : mHorizonInactiveFillPaint);
            drawLine(c, 0, radius, 2 * radius, radius);
            drawLine(c, radius, 0, radius, 2 * radius);
            c.drawCircle(radius, radius, elevationToRadius(s, 60), mGridStrokePaint);
            c.drawCircle(radius, radius, elevationToRadius(s, 30), mGridStrokePaint);
//            c.drawCircle(radius, radius, elevationToRadius(s, 0), mGridStrokePaint);
            c.drawCircle(radius, radius, radius, mHorizonStrokePaint);
        }

        private void drawTickLabel(Canvas c) {
            //绘制第一条最上面的刻度
            int mTikeWidth = 20;
            int mTikeCount = 12;
            int yPadding = 30;

//            c.drawLine(mWidth / 2, 0, mWidth / 2, mTikeWidth, mNorthPaint);
            //旋转的角度
            float rAngle = 360 / 12;
            //通过旋转画布 绘制右面的刻度
            for (int i = 1; i <= mTikeCount; i++) {
                double angle = (i - 3) * (Math.PI * 2) / 12;
                double x1 = mWidth / 2 + Math.cos(angle) * ((mWidth / 2 - yPadding)+10);
                double y1 = 0;
                if (i > 9 || i < 3) {
                    y1 = mWidth / 2 + Math.sin(angle) * ((mWidth / 2 - yPadding)+10);
                } else {
                    y1 = mWidth / 2 + Math.sin(angle) * ((mWidth / 2));
                }
                String text = "";
                if (i == 12) {
                    text = "0";
                } else {
                    text = "" + ((i) * 30);
                }
                c.drawText(text, ((i > 6) ? ((float) x1 - yPadding / 2)+28 : (float) x1)-20,
                        (float) y1, mPrnIdPaint);
            }

        }

        private void drawSatellite(Canvas c, int s, int elev, int azim, int snr, int svId, int sateType, boolean bUsed) {
            double radius, angle;
            float x, y;
            int bmpSel;
            int bmpSe2;

            // Place PRN text slightly below drawn satellite
            final double PRN_X_SCALE = 1.4;
            final double PRN_Y_SCALE = 3.5;

            Paint thisPaint = new Paint(mSatelliteFillPaint);
            if (bUsed) {
                thisPaint.setColor(Color.GREEN);
                bmpSel = 0;
            } else {
                thisPaint.setColor(Color.RED);
                bmpSel = 1;
            }

            Paint thisPaint2 = new Paint(mSatelliteFillPaint);
            if (bUsed) {
                thisPaint2.setColor(Color.GREEN);
                bmpSe2 = 1;
            } else {
                thisPaint2.setColor(Color.RED);
                bmpSe2 = 0;
            }


            radius = elevationToRadius(s, elev);
            azim -= mOrientation;
            angle = (float) Math.toRadians(azim);

            x = (float) ((s / 2) + (radius * Math.sin(angle)));
            y = (float) ((s / 2) - (radius * Math.cos(angle)));

            BeidouSatellite beidouSatellite = new BeidouSatellite(svId, snr, elev, azim, sateType);

            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            int r = bmpSatellite[BeidouSatellite.SATE_GPS][0].getHeight() / 2;
            switch (sateType) {
                case GnssSatellite.SATE_GLONASS:
                    rect = new Rect((int) -x - SAT_RADIUS, (int) y - SAT_RADIUS, (int) x + SAT_RADIUS, (int) y + SAT_RADIUS);
                    c.drawBitmap(bmpSatellite[BeidouSatellite.SATE_GLONASS][bmpSel], x - r, y - r, paint);


                    break;
                case GnssSatellite.SATE_GPS:
                    rect = new Rect((int) -x - SAT_RADIUS, (int) y - SAT_RADIUS, (int) x + SAT_RADIUS, (int) y + SAT_RADIUS);
                    c.drawBitmap(bmpSatellite[BeidouSatellite.SATE_GPS][bmpSel], x - r, y - r, paint);

                    break;
                case GnssSatellite.SATE_BEIDOU:
                    rect = new Rect((int) -x - SAT_RADIUS, (int) y - SAT_RADIUS, (int) x + SAT_RADIUS, (int) y + SAT_RADIUS);
                    c.drawBitmap(bmpSatellite[BeidouSatellite.SATE_BEIDOU][bmpSel], x - r, y - r, paint);

                    break;
                case BeidouSatellite.SATE_GALILEO:
                    rect = new Rect((int) -x - SAT_RADIUS, (int) y - SAT_RADIUS, (int) x + SAT_RADIUS, (int) y + SAT_RADIUS);
                    c.drawBitmap(bmpSatellite[BeidouSatellite.SATE_GALILEO][bmpSel], x - r, y - r, paint);
                    break;
                case BeidouSatellite.SATE_QZSS:
                    rect = new Rect((int) -x - SAT_RADIUS, (int) y - SAT_RADIUS, (int) x + SAT_RADIUS, (int) y + SAT_RADIUS);
                    c.drawBitmap(bmpSatellite[BeidouSatellite.SATE_QZSS][bmpSe2], x - r, y - r, paint);

                    break;
            }

            c.drawText(String.valueOf(svId), svId < 10 ? x - 4 : (svId < 100 ? x - 8 : x - 10), y + r + 12, mPrnNumPaint);
        }

        private float elevationToRadius(int s, int elev) {
            return ((s / 2) - SAT_RADIUS) * (1.0f - (elev / 90.0f));
        }

        private void drawTriangle(Canvas c, float x, float y, Paint fillPaint) {
            float x1, y1;  // Top
            x1 = x;
            y1 = y - SAT_RADIUS;

            float x2, y2; // Lower left
            x2 = x - SAT_RADIUS;
            y2 = y + SAT_RADIUS;

            float x3, y3; // Lower right
            x3 = x + SAT_RADIUS;
            y3 = y + SAT_RADIUS;

            Path path = new Path();
            path.setFillType(Path.FillType.EVEN_ODD);
            path.moveTo(x1, y1);
            path.lineTo(x2, y2);
            path.lineTo(x3, y3);
            path.lineTo(x1, y1);
            path.close();

            c.drawPath(path, fillPaint);
            c.drawPath(path, mSatelliteStrokePaint);
        }

        private void drawPentagon(Canvas c, float x, float y, Paint fillPaint) {
            Path path = new Path();
            path.moveTo(x, y - SAT_RADIUS);
            path.lineTo(x - SAT_RADIUS, y - (SAT_RADIUS / 3));
            path.lineTo(x - 2 * (SAT_RADIUS / 3), y + SAT_RADIUS);
            path.lineTo(x + 2 * (SAT_RADIUS / 3), y + SAT_RADIUS);
            path.lineTo(x + SAT_RADIUS, y - (SAT_RADIUS / 3));
            path.close();

            c.drawPath(path, fillPaint);
            c.drawPath(path, mSatelliteStrokePaint);
        }

        @Override
        protected void onDraw(Canvas canvas) {

            super.onDraw(canvas);
            //画布平移到中间
            canvas.translate(650, 0);
            int minScreenDimen;

            minScreenDimen = ((GpsSkyFragment.mWidth < GpsSkyFragment.mHeight)
                    ? GpsSkyFragment.mWidth : GpsSkyFragment.mHeight);

            drawHorizon(canvas, minScreenDimen);

//            drawNorthIndicator(canvas, minScreenDimen);

            drawTickLabel(canvas);

            if (mElevs != null) {
                int numSats = mSvCount;
                //绘制非北斗卫星
                for (int i = 0; i < numSats; i++) {
                    if (mSateType[i] == GnssSatellite.SATE_BEIDOU)
                        continue;
                    if (msvId[i] >= 201 && msvId[i]<=210){
                        Log.i("msvPrn","========="+ msvId[i]);
                    }
                    if (mSnrs[i] > 0 && (mElevs[i] != 0 || mAzims[i] != 0)) {
                        drawSatellite(canvas, minScreenDimen, mElevs[i], mAzims[i], mSnrs[i],
                                msvId[i], mSateType[i], mUsed[i]);
                    }
                }
                //绘制北斗卫星，确保北斗卫星不会被其它卫星覆盖
                for (int i = 0; i < numSats; i++) {
                    if (mSateType[i] != GnssSatellite.SATE_BEIDOU)
                        continue;
                    if (mSnrs[i] > 0 && (mElevs[i] != 0 || mAzims[i] != 0)) {
                        drawSatellite(canvas, minScreenDimen, mElevs[i], mAzims[i], mSnrs[i],
                                msvId[i], mSateType[i], mUsed[i]);
                    }
                }
            }
        }
    }
}

